import { observable, action } from '@/miniprogram_npm/mobx-miniprogram/index';
import tools from '@/utils/tools';

export default observable({
    records: [],
    currentPage: 1,
    loadFinished: false,
    fileDataMap: new Map(),

    // 获取并添加历史记录
    appendRecords: action(function (payload) {
        const newRecords = payload.map((item) => ({
            ...item,
            requestTime: tools.timestampToDateTime(item.requestTime)
        }));
        this.records = [...this.records, ...newRecords];
    }),

    addCurrentPage: action(function () {
        this.currentPage = this.currentPage + 1;
    }),

    setLoadFinished: action(function (payload) {
        this.loadFinished = payload;
    }),

    addFileData: action(function (path, data) {
        this.fileDataMap.set(path, data);
    }),

    deleteRecord: action(function (id) {
        this.records = this.records.filter((item) => item.id !== id);
    }),

    resetAll: action(function () {
        this.records = [];
        this.currentPage = 1;
        this.loadFinished = false;
    }),

    getFileData(path) {
        if (!this.fileDataMap.has(path)) {
            return null;
        }
        return this.fileDataMap.get(path);
    }
});
